local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "agenthelper"
local playerauthdao =  require "playerauthdao"
local configdao = require "configdao"
local tabletool = require "tabletool"
local timetool = require "timetool"
local auth = require "auth"
local gamelog = require "gamelog"
local table = table
local processstate = require "processstate" 
require "enum"

local processing = processstate:new({timeout = 4})
local  AgentReglogin = {}

--[[
//请求登陆注册服务器
message RegLoginReq {
	optional Version version = 1;
	optional int32 auth_type = 2;
	optional string user_name = 3;
	optional string cpass = 4;
    optional string regmobileid = 5; // 注册设备id
    optional string option_data = 6; // 可选数据	
}

//响应登陆注册服务器
message RegLoginRes {
	optional int32 errcode = 1;    //错误原因 0表示成功
	optional string errcodedes = 2;//错误描述	
	optional int32 uid = 3;        //账号uid
	optional string user_name = 4;
	optional int32 expiretime = 5; //过期时间戳
	optional string token = 6;     //登陆游戏服务器的认证token
}
]]

function  AgentReglogin.process(session, source, fd, request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
		errcodedes = "",
	}
	local server = msghelper:get_server()

	if processing:is_processing() then
		filelog.sys_warning("AgentReglogin.process processing:is_processing")
		return
	end
	processing:set_process_state(true)

	local status, userinfo = playerauthdao.query_player_uidbyusername(request.user_name)
	if not status then
		responsemsg.errcode = EErrCode.ERR_ACCESSDATA_FAILED
		responsemsg.errcodedes = "登陆访问数据失败！"
		msghelper:send_resmsgto_client(fd, "RegLoginRes", responsemsg)
		processing:set_process_state(false)
		server:exit_agent()
		return		
	end

	if userinfo == nil then
		responsemsg.errcode = EErrCode.ERR_ACCESSDATA_FAILED
		responsemsg.errcodedes = "登陆访问数据失败！"
		msghelper:send_resmsgto_client(fd, "RegLoginRes", responsemsg)
		processing:set_process_state(false)
		server:exit_agent()
		return		
	end

	if userinfo.isblock ~= 0 then
		responsemsg.errcode = EErrCode.ERR_AUTH_LOCKED
		responsemsg.errcodedes = "您的账号已被锁定，请联系客服！"
		msghelper:send_resmsgto_client(fd, "RegLoginRes", responsemsg)
		processing:set_process_state(false)
		server:exit_agent()
		return				
	end

	local slat_pass = auth.generate_md5slat_pass(request.cpass)
	if slat_pass ~= userinfo.slat_pass then				
		--让认证失败
		responsemsg.errcode = EErrCode.ERR_AUTHENTICATION_FAILURE
		responsemsg.errcodedes = "登陆账号认证失败，请检查你的登陆密码！"
		msghelper:send_resmsgto_client(fd, "RegLoginRes", responsemsg)
		processing:set_process_state(false)
		server:exit_agent()
		return		
	end

	local now_time = timetool.get_time()
	responsemsg.expiretime = now_time + configdao.get_common_conf("tokentimeout")
	responsemsg.uid = userinfo.uid
	responsemsg.user_name = userinfo.user_name

	local signstr = responsemsg.expiretime.."&&"..responsemsg.uid.."&&"..responsemsg.user_name
	responsemsg.token = auth.generate_thirdauth_md5token(signstr)

	msghelper:send_resmsgto_client(fd, "RegLoginRes", responsemsg)
	processing:set_process_state(false)
	server:exit_agent()
end

return AgentReglogin
